package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.auth2.AccessToken;
import com.ruoyi.framework.auth2.OAuth2Authentication;
import com.ruoyi.labsafety.domain.LabSafetyCheckIndexItem;
import com.ruoyi.labsafety.service.ILabSafetyCheckIndexItemService;
import com.ruoyi.synchdata.SynchDataServiceImpl;
import com.ruoyi.system.service.IReceiptFormService;
//import com.sudytech.ucp.serv.test.Test141;

/**
 * 备品Controller
 * 
 * @author ruoyi
 * @date 2022-02-11
 */
@RestController("winnerTest")
@RequestMapping("/test")
public class TestController extends BaseController {
	//@Autowired
	//private IReceiptFormService receiptFormService;

	/*@GetMapping("/update")
	public AjaxResult getInfo() {
		ReceiptForm receiptForm = new ReceiptForm();
    	receiptForm.setInstanceId(Long.valueOf(11));
    	receiptForm.setScanoutNum(10);
    	receiptFormService.scanoutIventory(receiptForm);
		return AjaxResult.success(1);
	}
	@GetMapping("/long")
	public AjaxResult testLong(Long id){
		System.out.println(id);
		return AjaxResult.success(id);
	}*/
	@Autowired
	private OAuth2Authentication oauth2Authentication;
	@Autowired
	private SynchDataServiceImpl synchDataServiceImpl;
	@Autowired
	private ILabSafetyCheckIndexItemService labSafetyCheckIndexItemService;
	@Autowired
	private IReceiptFormService receiptFormService;
	@GetMapping("/user")
	public AjaxResult testGetAccessToken(@RequestParam String code){
		AccessToken token = oauth2Authentication.getAccessToken(code);
		return AjaxResult.success(oauth2Authentication.getUserId(token.getName()));
	}

	@GetMapping("/synchUser")
	public AjaxResult synchronizedUserData(){
		return AjaxResult.success(synchDataServiceImpl.synchUser());
	}
	
	@GetMapping("/synchDept")
	public AjaxResult synchronizedDeptData(){
		//Map<String, LabOrgnization> orgMap = SynchDataForNY.synchDepartmentData(1,300);
		return AjaxResult.success(synchDataServiceImpl.synchDept());
	}
	
	@GetMapping("/sequence")
	public AjaxResult sequence(String key) throws Exception{
		//pdfGeneratorRunner.run(id);
		return AjaxResult.success(receiptFormService.getSequence(key));
	}
	/*@GetMapping("/sendMsg")
	public AjaxResult sendMsg(String subject, String content, String type, String address) throws Exception{
        SendResult sendResult = NyMessageSendUtil.sendMsg(subject, content, type, address);
        return AjaxResult.success(sendResult);
	}*/
	
	@GetMapping("/initCheckPointer/")
	public AjaxResult testPdf(@RequestParam Long indexId) throws Exception{
		LabSafetyCheckIndexItem labSafetyCheckIndexItem = new LabSafetyCheckIndexItem();
		labSafetyCheckIndexItem.setIndexId(indexId);
		List<LabSafetyCheckIndexItem> checkItemList =labSafetyCheckIndexItemService.selectLabSafetyCheckIndexItemList(labSafetyCheckIndexItem);
		Map<String, LabSafetyCheckIndexItem>  itemMap = new HashMap<String, LabSafetyCheckIndexItem>();
		String currentNo = "";
		for(LabSafetyCheckIndexItem item : checkItemList){
			if(item.getItemNo()==null){
				item.setItemNo(currentNo);
			}else{
				currentNo = item.getItemNo();
			}
			itemMap.put(item.getItemNo(), item);
			if(item.getItemNo().indexOf(".")<0){
				item.setParentId(Long.valueOf("0"));
				item.setAncestors("0");
			}
			else{
				String parentNo = item.getItemNo().substring(0, item.getItemNo().lastIndexOf("."));
				LabSafetyCheckIndexItem parent = itemMap.get(parentNo);
				if(parent==null){
					throw new RuntimeException("数据保存的顺序出现问题，没找到节点:"+item.getItemNo()+"的父节点:"+parentNo);
				}
				item.setParentId(parent.getId());
				item.setAncestors(parent.getAncestors()+","+parent.getId());
				
			}
			labSafetyCheckIndexItemService.updateLabSafetyCheckIndexItem(item);
		}
		return AjaxResult.success("finished");
	}
	
public static void main(String[] args) throws Exception{
	/*JSONObject jsonObject=new JSONObject();
	String data="{\"status\":200,\"msg\":\"successful\",\"total\":35106,\"data\":{\"Total\":10,\"Rows\":[{\"sjly\":\"教职工\",\"xgh\":\"20100065\",\"xm\":\"蒋元\",\"xznj\":\"\",\"yxdm\":\"301000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"通信与信息工程学院\",\"xb\":\"女\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"1057903232\",\"sflb\":\"教职工\",\"pagerownumber\":\"1\"},{\"sjly\":\"教职工\",\"xgh\":\"20060108\",\"xm\":\"孟飞\",\"xznj\":\"\",\"yxdm\":\"307000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"理学院\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"2444038229\",\"sflb\":\"教职工\",\"pagerownumber\":\"2\"},{\"sjly\":\"教职工\",\"xgh\":\"20040010\",\"xm\":\"王诚\",\"xznj\":\"\",\"yxdm\":\"301000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"通信与信息工程学院\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"2444075557\",\"sflb\":\"教职工\",\"pagerownumber\":\"3\"},{\"sjly\":\"教职工\",\"xgh\":\"20110025\",\"xm\":\"盛宇波\",\"xznj\":\"\",\"yxdm\":\"325000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"电子与光学工程学院、柔性电子（未来技术）学院\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"2444112133\",\"sflb\":\"教职工\",\"pagerownumber\":\"4\"},{\"sjly\":\"教职工\",\"xgh\":\"19840001\",\"xm\":\"许公全\",\"xznj\":\"\",\"yxdm\":\"1000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"党委办公室、校长办公室、保密委员会办公室（合署办公）\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"2444033269\",\"sflb\":\"教职工\",\"pagerownumber\":\"5\"},{\"sjly\":\"教职工\",\"xgh\":\"20060100\",\"xm\":\"朱金龙\",\"xznj\":\"\",\"yxdm\":\"124400\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"国家大学科技园管理办公室（科研院）\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"1100632841\",\"sflb\":\"教职工\",\"pagerownumber\":\"6\"},{\"sjly\":\"教职工\",\"xgh\":\"40100033\",\"xm\":\"武红\",\"xznj\":\"\",\"yxdm\":\"307000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"理学院\",\"xb\":\"女\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"\",\"sflb\":\"外聘人员\",\"pagerownumber\":\"7\"},{\"sjly\":\"教职工\",\"xgh\":\"207932\",\"xm\":\"陶鹏\",\"xznj\":\"\",\"yxdm\":\"306000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"材料科学与工程学院\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"2472387669\",\"sflb\":\"博士后\",\"pagerownumber\":\"8\"},{\"sjly\":\"教职工\",\"xgh\":\"40100040\",\"xm\":\"许杰\",\"xznj\":\"\",\"yxdm\":\"325000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"电子与光学工程学院、柔性电子（未来技术）学院\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"\",\"sflb\":\"外聘人员\",\"pagerownumber\":\"9\"},{\"sjly\":\"教职工\",\"xgh\":\"20060037\",\"xm\":\"李养群\",\"xznj\":\"\",\"yxdm\":\"309000\",\"zydm\":\"\",\"zy\":\"\",\"yx\":\"物联网学院\",\"xb\":\"男\",\"bh\":\"\",\"sfzx\":\"是\",\"wlkh\":\"2602636106\",\"sflb\":\"教职工\",\"pagerownumber\":\"10\"}]},\"page\":1,\"pagesize\":10}";
	JSONObject obj = jsonObject.parseObject(data);
	List<JSONObject> userObjectList = (List<JSONObject>)((JSONObject)obj.get("data")).get("Rows");
	userObjectList.forEach(item->{
		System.out.println(item.getString("xgh")+" "+item.getString("xm"));
	});*/
	//System.out.println(userObjectList.size());
	/*for(int i=3;i>=0;i--){
		System.out.println(i);
	}*/
}
	
	
}
